

$(function () {
    layui.use('form', function () {
        var form = layui.form;
        // layer.msg('玩命卖萌中', function(){
        //   //关闭后的操作
        //   });
        //监听提交
        form.on('submit(login)', function (data) {
            // alert(888)
            let filed ={
                username: $("#username").val(),
                password:$("#password").val(),
                code:$("#code").val()
            };
            console.log(filed)
            $.ajax({
                url:'/login',
                type: 'post',
                data:filed,
                dataType:'json',
                success: function (data) {
                    console.log(data)
                    let result = data.result;
                    if (result == 'error-code'){
                        layer.msg("验证码不正确");
                    }
                    if (result == 'error'){
                        layer.msg("用户名或密码错误");
                    } if (result == 'success'){
                        layer.msg("即将进入管理页面", function () {
                            //登录成功以后，需要将用户名传递到另一个页面
                            //js提供了两个容器，session  local
                            sessionStorage.setItem("loginName", $("#username").val());
                            localStorage.setItem("loginName", $("#username").val());
                            window.open("/html/index.html", "_self");
                        })
                    }
                },
                error:function (data) {
                    layer.msg("内部错误");
                    console.log(data)
                }
            })

            return false;
        });
        //自定义验证规则
        form.verify({
            username: [/^[\w-]{4,16}$/, "你输入的格式不正确"],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            code: [/(.+){1}/,'验证码不能为空'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

    });
    $('#code_img').click(function (e) {
        $("#code_img").attr('src', "/code?random=" + Math.random());
    });
})



//百度统计可去掉
var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();
